﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Microsoft.Xna.Framework;
using AAEngine.Box2D;
using AAEngine.Engine;
using AAEngine.Engine.Components;
using AAEngine.Engine.Core;
using AAEngine.Engine.Entities;

namespace Box2DTest.Components
{
    public class DudeController : TickedComponent
    {
        private InputMap _inputMap;
        private float _left, _right, _jump;
        private int _onground;

        public PropertyReference VelocityReference { get; set; }

        public InputMap Input
        {
            get { return _inputMap; }
            set
            {
                _inputMap = value;

                if (_inputMap != null)
                {
                    _inputMap.MapActionToHandler("GoLeft", OnLeft);
                    _inputMap.MapActionToHandler("GoRight", OnRight);
                    _inputMap.MapActionToHandler("Jump", OnJump);
                }
            }
        }

        public override void OnAdd()
        {
            base.OnAdd();

            Owner.EventDispatcher.AddEventListener(CollisionEvent.COLLISION_EVENT, OnCollision);
            Owner.EventDispatcher.AddEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, OnCollisionEnd);
        }

        public override void OnRemove()
        {
            Owner.EventDispatcher.RemoveEventListener(CollisionEvent.COLLISION_EVENT, OnCollision);
            Owner.EventDispatcher.RemoveEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, OnCollisionEnd);

            base.OnRemove();
        }

        public override void OnReset()
        {
            base.OnReset();
        }

        public override void OnTick(float deltaTime)
        {
            float move = _right - _left;
            Vector2 velocity = (Vector2)Owner.GetProperty(VelocityReference);
            velocity.X = move * 100;

            if (_jump > 0)
            {
                velocity.Y -= 200;
                _jump = 0;
            }

            Owner.SetProperty(VelocityReference, velocity);
        }

        private void OnLeft(float value)
        {
            _left = value;
        }

        private void OnRight(float value)
        {
            _right = value;
        }

        private void OnJump(float value)
        {
            if (_onground > 0)
            {
                _jump = value;
            }
        }

        private void OnCollision(Event ev)
        {
            CollisionEvent cev = (CollisionEvent)ev;
            if (AAGame.ObjectTypeManager.DoesTypeOverlap(cev.Collidee.CollisionType, "Platform"))
            {
                if (cev.Normal.Y > 0.7)
                {
                    _onground++;
                }
            }

            if (AAGame.ObjectTypeManager.DoesTypeOverlap(cev.Collider.CollisionType, "Platform"))
            {
                if (cev.Normal.Y < -0.7)
                {
                    _onground++;
                }
            }
        }

        private void OnCollisionEnd(Event ev)
        {
            CollisionEvent cev = (CollisionEvent)ev;
            if (AAGame.ObjectTypeManager.DoesTypeOverlap(cev.Collidee.CollisionType, "Platform"))
            {
                if (cev.Normal.Y > 0.7)
                {
                    _onground--;
                }
            }

            if (AAGame.ObjectTypeManager.DoesTypeOverlap(cev.Collider.CollisionType, "Platform"))
            {
                if (cev.Normal.Y < -0.7)
                {
                    _onground--;
                }
            }
        }
    }
}
